package com.example.myapplication;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class PlayerActivity extends Activity {
    private VideoView vv_show;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        vv_show=findViewById(R.id.vv_show);
        //得到播放地址
        uri=getIntent().getData();
        if (uri!=null){
            vv_show.setVideoURI(uri);
            Log.e("PlayerActivity",uri+"======");
        }
        //设置控制面板
        vv_show.setMediaController(new MediaController(this));
        //设置监听，用于视频加载缓冲
        vv_show.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                vv_show.start();
            }
        });
        //播放出错监听
        vv_show.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Toast.makeText(PlayerActivity.this,"播放视频出错",Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        //播放完成监听
        vv_show.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText(PlayerActivity.this,"播放完成",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}